/**
 * 
 */
package org.xelnaga.defiler.web.controller.folder;

import java.util.ArrayList;
import java.util.List;

import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
import org.xelnaga.defiler.domain.FolderModel;
import org.xelnaga.defiler.domain.SiteModel;

/**
 * 
 * Rest service message for folder model.
 * @author qijb
 *
 */
@JsonSerialize(include = Inclusion.NON_NULL)
public class FolderMessage {
	private Long inode;
	private String name;
	private String type;
	private String $ref;
	private Boolean hasChildren;
	private int depth;
	private List<FolderMessage> children;
	
	public FolderMessage() {
		
	}
	/**
	 * @param host 
	 * 
	 */
	public FolderMessage(SiteModel host) {
		if(null != host) {
			this.setInode(host.getInode());
			this.setName(host.getName());
			this.setHasChildren(true);
			this.setType("host");
		}
	}

	public FolderMessage(FolderModel folder) {
		if(null != folder) {
			this.setInode(folder.getInode());
			this.setName(folder.getTitle());
			this.setType("folder");
			this.setHasChildren(false);
		}
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public List<FolderMessage> getChildren() {
		return children;
	}

	public void setChildren(List<FolderMessage> children) {
		this.children = children;
	}

	public Long getInode() {
		return inode;
	}

	public void setInode(Long inode) {
		this.inode = inode;
	}

	public String get$ref() {
		return $ref;
	}

	public void set$ref(String $ref) {
		this.$ref = $ref;
	}

	public Boolean getHasChildren() {
		return hasChildren;
	}

	public void setHasChildren(Boolean hasChildren) {
		this.hasChildren = hasChildren;
	}
	
	public void addChild(FolderMessage child) {
		if(null == this.getChildren()) {
			this.children = new ArrayList<FolderMessage>();
		}
		child.set$ref(String.valueOf(child.getInode()));
		this.getChildren().add(child);
	}
	
	@Override
	public String toString() {
		return "FolderMessage [inode=" + inode + ", name=" + name + ", type="
				+ type + ", $ref=" + $ref + ", hasChildren=" + hasChildren
				+ ", children=" + children + "]";
	}
	public int getDepth() {
		return depth;
	}
	public void setDepth(int depth) {
		this.depth = depth;
	}
	public String getLeveledName() {
		StringBuffer sb = new StringBuffer();

		if(null == this.hasChildren || this.hasChildren) {
			sb.append("+--");
		} else {
			sb.append("---");
		}
		
		// 打印缩进
		for (int i = 0; i < this.getDepth(); i++) {
			sb.append("--");
		}
		sb.append(this.getName());
		return sb.toString();
	}
}
